package com.yao.service.Impl;

import com.yao.context.BaseContext;
import com.yao.mapper.UserMapper;
import com.yao.pojo.ChangeUser;
import com.yao.pojo.User;
import com.yao.service.UserService;
import com.yao.utils.AliOSSUtils;
import com.yao.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 用户注册
     * @param userParam
     * @return
     */
    @Override
    public Boolean userRegister(User userParam) {
        userParam.setPassword(DigestUtils.md5DigestAsHex(userParam.getPassword().getBytes()));
        User newUser = User.builder()
                .username(userParam.getUsername())
                .password(userParam.getPassword())
                .build();
        User user = userMapper.findEqualUser(newUser);

        if (!Objects.isNull(user)) return false;

        newUser.setCreateTime(LocalDateTime.now());
        newUser.setUpdateTime(LocalDateTime.now());

        userMapper.userRegister(newUser);

        return true;
    }

    /**
     * 用户登录
     * @param userParam
     * @return
     */
    @Override
    public String userLogin(User userParam) {
        userParam.setPassword(DigestUtils.md5DigestAsHex(userParam.getPassword().getBytes()));
        User userNameAndPassword = User.builder()
                .username(userParam.getUsername())
                .password(userParam.getPassword())
                .build();
        User user = userMapper.findEqualUser(userNameAndPassword);

        if (Objects.isNull(user)) return new String();

        Map<String,Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        claims.put("nickname", user.getNickname());
        claims.put("email", user.getEmail());

        String jwt = JwtUtils.generateJwt(claims);
        return jwt;
    }

    /**
     * 获取用户信息
     * @return
     */
    @Override
    public User getUserInfo() {
        // 获取当前用户ID
        Integer userId = BaseContext.getCurrentId();
        User user = User.builder()
                .id(userId)
                .build();
        User userInfo = userMapper.findEqualUser(user);
        return userInfo;
    }

    /**
     * 修改用户信息
     * @param user
     */
    @Override
    public void changeUserInfoByUserId(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.changeUserInfo(user);
    }

    @Override
    public Boolean changeUserPassword(ChangeUser changeUser) {
        if (!(changeUser.getNew_pwd().equals(changeUser.getOld_pwd()))) return false;

        User user = User.builder()
                .id(BaseContext.getCurrentId())
                .password(changeUser.getNew_pwd())
                .build();
        userMapper.changeUserInfo(user);
        return true;
    }

}









